package com.example.questionexam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.questionexam.common.ErrorCode;
import com.example.questionexam.exception.BusinessException;
import com.example.questionexam.exception.ThrowUtils;
import com.example.questionexam.mapper.AnswerMapper;
import com.example.questionexam.model.entity.Answer;
import com.example.questionexam.service.AnswerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author cola
* @description 针对表【answer(答案)】的数据库操作Service实现
* @createDate 2024-05-27 19:27:21
*/
@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer>
    implements AnswerService{

    @Override
    public void validQuestion(Answer answer, boolean isAdd) {
        ThrowUtils.throwIf(answer == null, ErrorCode.PARAMS_ERROR);
        Long id = answer.getId();
        Long questionId = answer.getQuestionId();
        String solution = answer.getSolution();
        String picture = answer.getPicture();
        if (isAdd) {
            ThrowUtils.throwIf(StringUtils.isAllBlank(solution, picture), ErrorCode.OPERATION_ERROR, "未添加任何答案");
        } else {
            ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        }
        if (questionId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "题目不存在");
        }
        if (StringUtils.isNotBlank(solution) && solution.length() > 400) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "答案过长");
        }
    }
}




